<template>
	<el-dialog title="新增文件夹" append-to-body :visible.sync="visible" width="640px">
		<el-form size="medium" ref="form" :model="formData" :rules="formRules" label-width="80px" class="folder_form_wrapper">
			<el-form-item label="目录名称" prop="name">
				<el-input v-model="formData.name" placeholder="智能目录名称"></el-input>
			</el-form-item>
			<el-form-item label="目录范围" prop="dirIds">
				<el-select v-model="formData.dirIds" placeholder="请选择智能目录范围" style="width: 100%">
					<el-option v-for="(item, index) in resLibrarys" :key="index" :label="item.name" :value="item.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="查询规则" prop="conditionJson">
				<dir-query-rules v-model="formData.conditionJson"></dir-query-rules>
			</el-form-item>
			<el-form-item label="目录描述" prop="content">
				<el-input type="textarea" placeholder="智能目录描述" :autosize="{ minRows: 3, maxRows: 6 }" v-model="formData.content"></el-input>
			</el-form-item>
		</el-form>
		<template slot="footer">
			<el-button size="medium" @click="visible = false">取消</el-button>
			<el-button size="medium" type="primary" :loading="btnLoading" @click="handleSubmitForm">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
import dirQueryRules from './rules';
import { getLibraryList } from '@/api/data/tbNetDiskDir';
import { aiDirDetail, addedAiDir, updateAiDir } from '@/api/data/tbNetAiDir';
export default {
	components: { dirQueryRules },
	props: { value: Boolean, dataId: String },
	data() {
		return {
			visible: this.value,
			formData: {},
			formRules: {
				name: [{ required: true, message: '请输入智能目录名称', trigger: 'blur' }]
				/* dirIds: [{ required: true, message: '请选择智能目录范围', trigger: 'change' }],
				conditionJson: [{ required: true, message: '请选择智能目录查询规则', trigger: 'change' }] */
			},
			resLibrarys: [],
			btnLoading: false
		};
	},
	computed: {
		addOrEdit() {
			return !this.dataId;
		}
	},
	watch: {
		value(flag) {
			this.visible = flag;
		},
		async visible(flag) {
			this.$emit('input', flag);
			if (!flag) return false;
			await this.getLibraryListData();
			this.$nextTick(async () => {
				this.formData = this.addOrEdit ? {} : await this.getDataDetail(this.dataId);
				this.$refs['form'].clearValidate();
			});
		}
	},
	methods: {
		//获取资源库列表
		async getLibraryListData() {
			let params = { page: 1, limit: 9999, type: '1', libraryId: '-1' };
			let result = await getLibraryList(params).then((res) => res.data);
			if (result.code === '0') {
				this.resLibrarys = result.data || [];
			}
		},

		//获取数据详情
		async getDataDetail(id) {
			let result = await aiDirDetail(id).then((res) => res.data);
			if (result.code === '0') {
				let datas = this.$detailData(result.data);
				return Promise.resolve(datas);
			}
		},

		handleSubmitForm() {
			this.$refs['form'].validate(async (valid) => {
				if (!valid) return false;
				this.btnLoading = true;
				// await this.createFolderRequest();
				this.btnLoading = false;
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.folder_form_wrapper {
	width: calc(100% - 10px);
	padding: 10px 10px 0 0;
}
</style>